<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>新增</title>
</head>

<body>
    <table id="one">
        <tr id="two">
            <td>添加博客或者修改</td>
        </tr>
        <tr>
            <td><label for="">标题：</label></td>
            <td><input type="text" name="title" placeholder="请输入标题"></td>
        </tr>
        <tr>
            <td><label for="">摘要：</label></td>
            <td><input type="text" name="digest" placeholder="请输入摘要"></td>
        </tr>
        <tr>
            <td><label for="">内容：</label></td>
            <td><input type="text" name="matter" placeholder="请输入内容"></td>
        </tr>
        <tr>
            <td><label for="">分类：</label></td>
            <td><input type="text" name="classify" placeholder="请输入分类"></td>
        </tr>
        <tr>
            <td><label for="">作者：</label></td>
            <td><input type="text" name="author" placeholder="请输入作者"></td>
        </tr>   
        <tr id="last">
            <td><input type="button" value="保存" onclick="save()"></td>
            <td><input type="button" value="取消" onclick="cancel()"></td>
        </tr>
    </table>

    <script src="./js/jquery-3.6.0.min.js"></script>
    <script src="./js/api.js"></script>
    <script>
        $(function () {
            let id = getUrlParam('id');
            console.log(id);
            getProductById(id).then(res => {
                console.log(res);
                let obj = res.data;
                $('[name=title]').val(obj.title);
                $('[name=digest]').val(obj.digest);
                $('[name=matter]').val(obj.matter);
                $('[name=classify]').val(obj.classify);
                $('[name=author]').val(obj.author)
            })
        })
        function getUrlParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");

            var r = window.location.search.substr(1).match(reg);

            if (r != null) return unescape(r[2]); return null;
        }
        function save() {
            let id = getUrlParam('id');
            console.log(id);
            let obj = {
                title: $('[name=title]').val(),
                digest: $('[name=digest]').val(),
                matter: $('[name=matter]').val(),
                classify: $('[name=classify]').val(),
                author: $('[name=author]').val()
            }
            if (id) {
                updateProduct(id, obj).then(res => {
                    console.log(res);
                    location.href = './index.html'
                })
            } else {
                let params = {
                    title: obj.title,
                    digest: obj.digest,
                    matter: obj.matter,
                    classify: obj.classify,
                    author: obj.author
                }
                addProduct(params).then(res => {
                    location.href = './index.html'
                })
            }
        }
        function cancel() {
            location.href = './index.html'
        }
    </script>

</body>

</html>